﻿// <copyright file="GameInformationGM.cs" company="Gravity Age Studios">
// Code released under the MIT license (see License.txt).
// </copyright>
// <author>Vicente Cartas Espinel</author>
// <email>vicente.cartas@gmail.com</email>
namespace LightSwitchApplication
{
    using System;
    using LightSwitchApplication.Export;
    using LightSwitchApplication.Import;
    using LightSwitchApplication.UserCode;
    using Microsoft.LightSwitch.Presentation.Extensions;

    public partial class GameInformationGM
    {
        private bool isNewInformation;

        #region Add/Edit GameInformation

        partial void gridAddAndEditNew_Execute()
        {
            this.isNewInformation = true;

            var info = this.Collection.AddNew();
            info.Visibility = (int)VisibilityEnum.Public;
            info.CreationDate = DateTime.Now;

            this.FindControl("ModalWindowGameInformation").DisplayName = "Add new Game Information";
            this.OpenModalWindow("ModalWindowGameInformation");
        }

        partial void gridEditSelected_CanExecute(ref bool result)
        {
            result = this.Collection.SelectedItem != null;
        }

        partial void gridEditSelected_Execute()
        {
            this.isNewInformation = false;

            this.FindControl("ModalWindowGameInformation").DisplayName = string.Format("Edit {0}", this.Collection.SelectedItem.Title);
            this.OpenModalWindow("ModalWindowGameInformation");
        }

        partial void SubmitModalWindowGameInformation_Execute()
        {
            this.CloseModalWindow("ModalWindowGameInformation");

            if (this.Collection.SelectedItem.Details.EntityState != Microsoft.LightSwitch.EntityState.Unchanged)
            {
                this.DataWorkspace.ApplicationData.SaveChanges();

                // If the information is public, inform all players of it
                if (this.Collection.SelectedItem.Visibility != (int)VisibilityEnum.GameMaster)
                {
                    foreach (var player in this.Players)
                    {
                        var notification = this.Notifications.AddNew();
                        notification.Read = false;
                        notification.SentDate = DateTime.Now;
                        notification.Recipient = player;

                        if (isNewInformation)
                        {
                            notification.Title = "New Game Information created";
                            notification.Message = string.Format("A new game information with the title \"{0}\" has been created.", this.Collection.SelectedItem.Title);
                        }
                        else
                        {
                            notification.Title = "Game Information updated";
                            notification.Message = string.Format("A game information with the title \"{0}\" has been updated.", this.Collection.SelectedItem.Title);
                        }
                    }

                    this.DataWorkspace.ApplicationData.SaveChanges();
                }
            }
        }

        partial void CancelModalWindowInformation_Execute()
        {
            this.CloseModalWindow("ModalWindowGameInformation");
            this.Collection.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        partial void Import_Execute()
        {
            ImporterHelper.ImportGameInformation(this.DataWorkspace, this.Collection);
        }

        partial void Export_Execute()
        {
            Exporter.ExportGameInformation(this.Collection);
        }
    }
}
